package cn.edu.sjtu.stap.util;

import java.util.*;

import org.aspectj.org.eclipse.jdt.internal.compiler.ast.*;

public class CallMap {
    private static CallMap instance = new CallMap();

    private Map<AbstractMethodDeclaration, List<ASTNode>> calls = new HashMap<AbstractMethodDeclaration, List<ASTNode>>();

    public static CallMap getInstance() {
        return instance;
    }

    public void clear() {
        calls.clear();
    }

    public void addCall(AbstractMethodDeclaration d, ASTNode call) {
        List<ASTNode> list = calls.get(d);
        if (null == list) {
            list = new ArrayList<ASTNode>();
            calls.put(d, list);
        }
        list.add(call);
    }

    public AbstractMethodDeclaration findCaller(ASTNode call) {
        Iterator<AbstractMethodDeclaration> mIt = calls.keySet().iterator();
        while (mIt.hasNext()) {
            AbstractMethodDeclaration m = mIt.next();
            List<ASTNode> list = calls.get(m);
            if (null != list && list.contains(call)) {
                return m;
            }
        }

        return null;
    }

    public List<ASTNode> getCalls(AbstractMethodDeclaration d) {
        return calls.get(d);
    }

    public Set<AbstractMethodDeclaration> getAllMethods() {
        return calls.keySet();
    }

    public Collection<List<ASTNode>> getAllCalls() {
        return calls.values();
    }
}
